<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="药品名称" prop="medicineName">
            <el-input
              v-model="formData.medicineName"
              placeholder="请输入药品名称"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="药品简码" prop="medicineBrevityCode">
            <el-input
              v-model="formData.medicineBrevityCode"
              placeholder="请输入药品简码"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型" prop="medicineType">
            <el-select v-model="formData.medicineType" placeholder="请输入类型" :clearable="true">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.HEALTH_MEDICINE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格" prop="medicineSpecification">
            <el-input
              v-model="formData.medicineSpecification"
              placeholder="请输入规格"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="剂型" prop="medicineDosageForm">
            <el-select
              v-model="formData.medicineDosageForm"
              placeholder="请输入剂型"
              :clearable="true"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.HEALTH_MEDICINE_DOSAGE_FORM)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂家" prop="manufacturer">
            <el-input
              v-model="formData.manufacturer"
              placeholder="请输入生产厂家"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="HealthMedicineEdit" lang="ts">
import * as HealthMedicineApi from '@/api/health/medicine'
import * as HealthMedicineModel from '@/model/health/medicine/HealthMedicine'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
const message = useMessage()
const { t } = useI18n()
let formType = ''
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  medicineName: '',
  medicineBrevityCode: '',
  medicineType: 1,
  medicineSpecification: '',
  medicineDosageForm: 1,
  manufacturer: ''
})
const formRules = reactive({
  medicineName: [{ required: true, message: '请输入药品名称', trigger: 'change' }],
  medicineType: [{ required: true, type: 'number', message: '请输入类型', trigger: 'change' }],
  medicineDosageForm: [{ required: true, type: 'number', message: '请输入剂型', trigger: 'change' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await HealthMedicineApi.singleGet(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as HealthMedicineModel.HealthMedicine
    if (formType === 'create') {
      await HealthMedicineApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await HealthMedicineApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    medicineName: '',
    medicineBrevityCode: '',
    medicineType: 1,
    medicineSpecification: '',
    medicineDosageForm: 1,
    manufacturer: ''
  }
  formRef.value?.resetFields()
}
</script>
